package com.lbc.bq;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;

//  阻塞队列
public class ArrayBlockingQueueDemo {
    public static void main(String[] args) throws InterruptedException {
        //  test1();
        //  test2();
        //  test3();
        test4();
    }

    /**
     *   抛出异常
     */
    public static void test1() {
        //  队列的大小
        ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);

        System.out.println(blockingQueue.add("a"));
        System.out.println(blockingQueue.add("b"));
        System.out.println(blockingQueue.add("c"));
        //  IllegalStateException: Queue full  抛出异常！
        //  System.out.println(blockingQueue.add("d"));

        System.out.println("======FIFO======");
        System.out.println("队首元素 = "+blockingQueue.element());
        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());
        //  NoSuchElementException  没有元素的异常
        //  System.out.println(blockingQueue.remove());
    }

    /**
     *   有返回值，没有异常
     */
    public static void test2() {
        //  队列的大小
        ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);

        System.out.println(blockingQueue.offer("a"));
        System.out.println(blockingQueue.offer("b"));
        System.out.println(blockingQueue.offer("c"));
        //  false  不抛出异常
        //  System.out.println(blockingQueue.offer("d"));
        System.out.println("队首元素 = "+blockingQueue.peek());
        System.out.println("=============");
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        //  null  也不抛出异常
        //  System.out.println(blockingQueue.poll());
    }

    /**
     *   等待，阻塞（一直阻塞）
     */
    public static void test3() throws InterruptedException {
        //  队列的大小
        ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);

        //  没有返回值，要主动抛出异常
        blockingQueue.put("a");
        blockingQueue.put("b");
        blockingQueue.put("c");
        //  blockingQueue.put("d");  //  队列没有位置了，一直等待

        System.out.println(blockingQueue.take());
        System.out.println(blockingQueue.take());
        System.out.println(blockingQueue.take());
        System.out.println(blockingQueue.take());  //  没有这个元素，也是一直阻塞
    }

    /**
     *   等待，阻塞（等待超时）
     */
    public static void test4() throws InterruptedException {
        //  队列的大小
        ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);

        blockingQueue.offer("a");
        blockingQueue.offer("b");
        blockingQueue.offer("c");
        //  blockingQueue.offer("d", 2, TimeUnit.SECONDS);  //  等待超过2秒就退出

        System.out.println("=============");

        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll(2, TimeUnit.SECONDS));  //  null  等待超过2秒就退出
    }
}
